from cassacoproject.cassaco.models import Category
from cassacoproject.cassaco.models import Subcategory
from cassacoproject.cassaco.views.chart import COLORS
from django.http import HttpResponse

def getCategoriesAndSubcategoriesDict():
    d = {}
    cats = Category.objects.all()
    for c in cats:
        d[c] = []
    
    subs = Subcategory.objects.all()
    for s in subs:
        cat = s.category
        d[cat] += [s,]
    return d

def getCategoriesAndSubcategoriesHTML(request):
    colors = COLORS[:]
    d = getCategoriesAndSubcategoriesDict()
    output = '<ul>'
    for c, ss in d.items():
        output += ("""<li>
                    <a href="/cassaco/category/delete/%d">[x]</a> 
                    <a href="#spentList" 
                    onclick='$("#spentList").load("/cassaco/spent/listByCategory/%d/");'
                    >%s</a>""") % (c.id, c.id, c.description)
        output += '<ul>'
        for s in ss:
            output += ("""<li><a href="/cassaco/subcategory/delete/%d">[x]</a> 
                       <a href="#spentList" 
                   onclick='$("#spentList").load("/cassaco/spent/listBySubcategory/%d/");'
                   >%s</a>""") % (s.id, s.id, s.description)
            output += '</li>'
        output += '</ul>'
        output += '</li>'
    output += '</ul>'
    return HttpResponse(output)